package top.csy8888.ch05.service;

import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import top.csy8888.ch05.config.RabbitConfig;
import top.csy8888.ch05.config.RabbitConfig;
import top.csy8888.ch05.dto.Order;

import java.util.UUID;

/**
 * @author CSY
 * @date 2025 - 10 - 11
 */
@Service
@RequiredArgsConstructor
public class OrderProducerService {
    /**
     * 注入RabbitTemplate来投递消息
     */
    private final RabbitTemplate rabbitTemplate;

    /**
     * 投递消息
     * @param message
     */
    public void publish(String message){
        //创建消息的唯一ID，用于区分消息
        CorrelationData correlationData = new CorrelationData(message);
        correlationData.setId(UUID.randomUUID().toString());
        //消息的投递
        // 参数一：投递的交换机
        // 参数二：路由键
        // 参数三：具体的消息
        // 参数四：消息的唯一ID
        rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_NAME, RabbitConfig.ROUTING_KEY, message, correlationData);
    }

    /**
     * @param order
     */
    public void publish(Order order){

        //创建消息的唯一ID，用于区分消息
        CorrelationData correlationData = new CorrelationData();
        correlationData.setId(UUID.randomUUID().toString());
        //消息的投递
        // 参数一：投递的交换机
        // 参数二：路由键
        // 参数三：具体的消息
        // 参数四：消息的唯一ID
        rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_NAME, RabbitConfig.ROUTING_KEY, order, correlationData);
    }
}
